package com.efast.cafe.portal.controller.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.vo.DataTableVo;
import com.efast.cafe.portal.bean.api.ApiOutputParamQueryBean;
import com.efast.cafe.portal.entity.api.ApiInputParam;
import com.efast.cafe.portal.entity.api.ApiOutputParam;
import com.efast.cafe.portal.service.api.IApiOutputParamService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;
@Controller
@RequestMapping("/frame_center/api")
public class ApiOutputParamController extends AbstractController {
	
	@Autowired
	private IApiOutputParamService apiOutputParamService;
	
	
	


	@RequestMapping(value = "/outputParam/page")
	public String templatePage(@RequestParam(required = false) String searchValue, ModelMap model,
			HttpServletRequest request, HttpServletResponse response){
		
		return "/api/outputParam/list";
	}

	
	/**
	 * 查询uri参数列表 不分页
	 * @param uriId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/outputParam/list")
	public @ResponseBody DataTableVo<ApiInputParam> templatePage(@ModelAttribute ApiOutputParamQueryBean queryBean, 
			HttpServletRequest request, HttpServletResponse response){
		
		return outPageJSONArray_bootstrapDatatable(apiOutputParamService.queryPageList(queryBean));
	}
	
	/**
	 * 新增uri参数
	 * @param apiInputParam
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/outputParam/add")
	public @ResponseBody ResultBean addParam(@ModelAttribute ApiOutputParam apiOutputParam, 
			HttpServletRequest request, HttpServletResponse response){
		RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean(true, context.getMessage(ADD_SUCCESS_TIP));
		Integer id = null;
		try {
			
			id = apiOutputParamService.addParam(apiOutputParam);
			result.setObject(id.toString());
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(context.getMessage(e.getMessage()));
			} else {
				result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}

		return result;
	}
	
	/**
	 * 编辑uri参数
	 * @param apiInputParam
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/outputParam/edit")
	public @ResponseBody ResultBean editParam(@ModelAttribute ApiOutputParam apiOutputParam, 
			HttpServletRequest request, HttpServletResponse response){
		RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean(true, context.getMessage(EDIT_SUCCESS_TIP));
		try {
			
			apiOutputParamService.editParam(apiOutputParam);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(context.getMessage(e.getMessage()));
			} else {
				result.setMessage(context.getMessage(EDIT_FAILURE_TIP));
			}
		}

		return result;
	}
	
	/**
	 * 删除uri参数
	 * @param paramId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/outputParam/del")
	public @ResponseBody ResultBean delParam(@RequestParam(required = false) Integer paramId, 
			HttpServletRequest request, HttpServletResponse response){
		RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean(true, context.getMessage(DEL_SUCCESS_TIP));
		try {
			apiOutputParamService.delParam(paramId);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(context.getMessage(e.getMessage()));
			} else {
				result.setMessage(context.getMessage(DEL_FAILURE_TIP));
			}
		}
		return result;
	}
}
